/**
 * 
 */
package com.td.framework.webmanager.qxygns.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.td.framework.common.Constant;
import com.td.framework.utils.DateUtils;
import com.td.framework.webmanager.domain.AppsysQxygns;
import com.td.framework.webmanager.domain.AppsysYmygnsgx;
import com.td.framework.webmanager.qxygns.dao.QxygnsDao;

/**
 * 
 * Title: 权限与功能树数据层实现类 Description: 处理权限与功能树信息 Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * @author 
 * @version 1.0
 */
public class QxygnsDaoImpl extends HibernateDaoSupport implements QxygnsDao {
	private static final Log log = LogFactory.getLog(QxygnsDaoImpl.class);
	private String sql = "";// sql语句;
	private List<AppsysQxygns> qxygnsList = null;// 权限与功能树List;
	private boolean delStatus;// 删除记录标志 true:彻底删除；false:象征性删除;

	/**
	 * @toDo:删除权限与功能树信息,支持批量删除;
	 * @param ymbhs
	 * @return boolean;
	 */
	public boolean deleteAppsysQxygns(String[] ymbhs) {
		// TODO Auto-generated method stub

		boolean flag = false;
		sql = " update appsys_qxygns t set t.scbz=1 where 1=1 and ";
		// 删除页面功能与模块关系sql
		String ymgxSql = " update appsys_ymygnsgx t set t.scbz=1 where 1=1 and ";
		if (delStatus) {
			sql = " delete appsys_qxygns t where 1=1 and  ";
			ymgxSql = " delete appsys_ymygnsgx t where 1=1 and ";
		}
		String condition = "";
		for (int i = 0; i < ymbhs.length; i++) {
			if (i == ymbhs.length - 1) {
				condition += " t.ymbh like '" + ymbhs[i].trim() + "%'  ";
			} else {
				condition += " t.ymbh like '" + ymbhs[i].trim() + "%' or ";
			}

		}
		sql += condition;
		ymgxSql += condition;
		try {
			Session session = getSession();
			Connection con = session.connection();
			con.setAutoCommit(false);
			Statement st = con.createStatement();
			st.addBatch(sql);
			st.addBatch(ymgxSql);
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			con.close();
			// //session.close();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			log.error("Exception： QxygnsDaoImpl+deleteAppsysQxygns", e);
		}
		return flag;
	}

	/**
	 * @toDo:得到总记录数;
	 * @param sjbh,ymmc
	 * @return rows;
	 */
	@SuppressWarnings("unchecked")
	public int getRows() {
		int rows = 0;
		Session session = this.getSession();
		Query query = session.createQuery(sql);
		List list = query.list();
		rows = list.size();
		return rows;
	}

	/**
	 * @toDo:通过上级页面编号查询权限与功能树信息;
	 * @param pageSize,startRow
	 * @return qxygnsList;
	 */
	@SuppressWarnings("unchecked")
	public List findBySjbh(int pageSize, int startRow) {
		Session session = getSession();
		Query query = session.createQuery(getSql());
		query.setFirstResult(startRow);
		query.setMaxResults(pageSize);
		qxygnsList = query.list();
		// session.close();
		return qxygnsList;
	}

	/**
	 * @toDo:通过页面编号查询权限与功能树对象;
	 * @param ymbh
	 * @return qxygns;
	 */
	@SuppressWarnings("unchecked")
	public AppsysQxygns findByYmbh(String ymbh) {
		// TODO Auto-generated method stub
		sql = "from com.td.framework.webmanager.domain.AppsysQxygns t where  t.scbz=0 and t.ymbh='"
				+ ymbh + "'";
		qxygnsList = this.getHibernateTemplate().find(sql);
		AppsysQxygns appsysQxygns = null;
		if (qxygnsList.size() > 0) {
			appsysQxygns = (AppsysQxygns) qxygnsList.get(0);
		}
		return appsysQxygns;
	}

	/**
	 * @toDo 生成系统树;
	 * @return list;
	 */
	@SuppressWarnings("unchecked")
	public List getTreeList() {
		// TODO Auto-generated method stub
		sql = " from com.td.framework.webmanager.domain.AppsysQxygns t "
				+ "where t.scbz=0 and t.sfks=0 order by t.jibie,t.pxh,t.ymbh";
		qxygnsList = this.getHibernateTemplate().find(sql);
		return qxygnsList;
	}

	/**
	 * @toDo:生成权限树;
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getXmlTreeList() {
		sql = " from com.td.framework.webmanager.domain.AppsysQxygns t "
				+ "where t.scbz=0 and t.sfks=0 order by t.ymbh";
		qxygnsList = this.getHibernateTemplate().find(sql);
		return qxygnsList;
	}

	/**
	 * @toDo:生成与权限结合的功能树;
	 * @param yhzbh,
	 * @param flag 0表示用户组、1表示用户
	 * @param type 0 已授权,1 未授权,2 已授权带tab页
	 * @return list
	 */
	public List getRightTreeList(String yhzbh, Long flag, int type) {
		String[] yhzbhs =yhzbh.split(",");
		String newStrYhzbh="";
		for(String bh :yhzbhs){
			newStrYhzbh+="'"+bh+"',";
		}
		newStrYhzbh = newStrYhzbh.substring(0, newStrYhzbh.length()-1);
		// 生成系统授权时当前用户已授权的功能树;
		List<AppsysQxygns> listMyGns = new ArrayList<AppsysQxygns>();
	  if(type==0){	
		sql = "select distinct * from appsys_qxygns t where t.SCBZ='0' and t.sbsjd='1' and t.ymbh in (select t1.qxbh from appsys_yhzqx t1 where t1.yhzbh in("+newStrYhzbh+")) order by YMBH";
		// 生成系统授权时当前用户未授权的功能树;
	  }else if (type == 1) {
			sql = "select distinct * from appsys_qxygns t where t.SCBZ='0' and t.sbsjd='1' and  t.ymbh not in (select t1.qxbh from appsys_yhzqx t1 where t1.yhzbh in("+newStrYhzbh+")) order by YMBH";			
	  }
		Session session = this.getSession();		
		qxygnsList = session.createSQLQuery(sql).addEntity(AppsysQxygns.class).list();
		Set<String> set = new HashSet<String>();		
			//根据叶子节点们得到所有关联的父节点层次
			for(AppsysQxygns gns:qxygnsList){
				String[] jds = gns.getJdlj().split("/");
				for(String jd:jds){
					set.add(jd);
				}
			}
			List<String> list = new ArrayList<String>(set);
			Collections.sort(list);
			String hql="";
			for(String ymbh:list){
				 hql = "from AppsysQxygns where ymbh ='"+ymbh+"' and scbz='0'";				
				listMyGns.add((AppsysQxygns) this.getHibernateTemplate().find(hql).get(0));
			}
			return listMyGns;
	}
	/**
	 * @toDo:验证用户类型;
	 * @param yhbh
	 * @return flag
	 */
	@SuppressWarnings("unchecked")
	public boolean checkYhlx(String yhbh)
	{
		Session session = this.getSession();
		sql = "select t.id from appsys_xtbmqx t where "
				+ "t.scbz=0 and (t.yhbh='" + yhbh + "' and t.yhlb=1 or "
				+ "(t.yhbh in(select a.yhbhz from appsys_yhyhzgx a "
				+ "where a.scbz=0 and a.yhbh='" + yhbh + "') and t.yhlb=0 )) "
				+ "and t.xtbm='" + Constant.SYS_XTBM + "'";
		// 判断是否为子系统管理员
		boolean flag = false;
		List list = session.createSQLQuery(sql).list();
		if (list != null && list.size() > 0) {
            flag = true;
		}
		return flag ;
	}

	/**
	 * @toDo:用户登录成功左边菜单树;
	 * @param yhbh
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List getMainTreeList(String yhbh, String sjbh,boolean flag) {

		// 用户登录成功左边菜单树;
		sql = "select distinct t.* from appsys_qxygns t "
				+ "where t.scbz=0 and t.sfks=0 order by t.jibie,t.pxh,t.ymbh";
		Session session = this.getSession();
		if (!yhbh.equals("") && !yhbh.equals("null")) {
			if (!yhbh.equals("-1")&&flag!=true) {
				sql = "select distinct t.* from appsys_qxygns t,"
						+ "(select t.jdlj as jdlj  from appsys_qxygns t  "
						+ "where t.scbz=0 and t.sfks=0 "
						+ " and (t.xtbm in ('SYS','"
						+ Constant.SYS_XTBM
						+ "_SYS','"
						+ Constant.SYS_XTBM
						+ "')) "
						+ " and  t.mrgn<2 and t.ymbh in  "
						+ "(select t.qxbh from appsys_yhzqx t " +
								"where  t.xtbm = '"+Constant.SYS_XTBM+"' and  t.scbz=0 )  or t.mrgn=1) b "
						+ "where t.scbz=0 and t.sfks=0  and t.mrgn<2 "
						+ " and (t.xtbm in ('SYS','" + Constant.SYS_XTBM
						+ "_SYS','" + Constant.SYS_XTBM + "')) ";
				if (sjbh.endsWith("-1")) {
					sql += " and t.JIBIE>1 and b.jdlj like  concat(t.jdlj,'%')order by t.ymbh,t.pxh";
				} else if (sjbh.endsWith("-2")) {
					sql += " and b.jdlj like  concat(t.jdlj,'%')order by t.jibie,t.pxh,t.ymbh";
				} else {
					sql += " and (t.sjbh='"
							+ sjbh
							+ "') and b.jdlj like  concat(t.jdlj,'%')order by t.jibie,t.pxh,t.ymbh";
				}
			} else {
				sql = "select distinct t.* from appsys_qxygns t "
						+ "where t.scbz=0 and t.sfks=0 and jibie>1"
						+ " and (t.xtbm in ('SYS','" + Constant.SYS_XTBM+"')) "
						+ "  and t.mrgn<2 and ymbh like '"+sjbh+"%' order by t.ymbh, t.pxh";
			}
		}
		qxygnsList = session.createSQLQuery(sql).addEntity(AppsysQxygns.class)
				.list();
		// session.close();
		return qxygnsList;
	}
	/* (non-Javadoc)
	 * @see com.td.framework.webmanager.qxygns.dao.QxygnsDao#getMainTreeList(java.lang.String)
	 */
	public List getMainTreeList(String yhbh) {

		// 用户登录成功左边菜单树;
		sql = "select distinct t.* from appsys_qxygns t "
				+ "where t.scbz=0 and t.sfks=0 order by t.jibie,t.pxh,t.ymbh";
		Session session = this.getSession();
		if (!yhbh.equals("") && !yhbh.equals("null")) {
			if (!yhbh.equals("-1")) {
				sql = "select distinct t.* from appsys_qxygns t,"
						+ "(select t.jdlj as jdlj  from appsys_qxygns t  "
						+ "where t.scbz=0 and t.sfks=0 "
						+ " and (t.xtbm in ('SYS','"
						+ Constant.SYS_XTBM
						+ "_SYS','"
						+ Constant.SYS_XTBM
						+ "')) "
						+ " and (( t.mrgn<2 and t.ymbh in  "
						+ "(select t.qxbh from appsys_yhzqx t " +
								"where  t.xtbm = '"+Constant.SYS_XTBM+"' and  t.scbz=0 and t.yhzbh in (select d.yhbhz from appsys_yhyhzgx d  "
						+ "where d.scbz=0 and d.yhbh='" + yhbh
						+ "' )))" + ") )  or t.mrgn=1 )b "
						+ "where t.scbz=0 and t.sfks=0  and t.mrgn<2 "
						+ " and (t.xtbm in ('SYS','" + Constant.SYS_XTBM
						+ "_SYS','" + Constant.SYS_XTBM + "')) ";
			} else {
				sql = "select distinct t.* from appsys_qxygns t "
						+ "where t.scbz=0 and t.sfks=0 and t.ymbh < '0102' "
						+ " and (t.xtbm in ('SYS','" + Constant.SYS_XTBM
						+ "_SYS')) "
						+ "  and t.mrgn<2  order by t.jibie,t.pxh,t.ymbh";
			}
		}
		qxygnsList = session.createSQLQuery(sql).addEntity(AppsysQxygns.class)
				.list();
		// session.close();
		return qxygnsList;
	}
	
	/**
	 *得到首页的主要大功能菜单 
	 *@return 首页大功能
	 */
	public List getMainFunc(String yhbh){
		String hql ="";
		if("-1".equals(yhbh)){//管理员
			hql = "from AppsysQxygns where sjbh='-1' and xtbm in ('SYS','" + Constant.SYS_XTBM+"') order by pxh";
		}else{
			hql = "from AppsysQxygns where sjbh='-1' and xtbm in ('SYS','" + Constant.SYS_XTBM
			+ "_SYS') and ymbh in (select qxbh from AppsysYhzqx where yhzbh in (select yhbhz from AppsysYhyhzgx where yhbh='"+yhbh+"')) order by pxh";
		}
		return this.getHibernateTemplate().find(hql);
	}
	
	/**
	 * @toDo:通过页面编号生成功能树
	 * @param ymbhs
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getRightTreeList(String[] ymbhs) {
		qxygnsList = null;
		if (ymbhs != null) {
			String condition = " and ( t.ymbh in (";
			for (int i = 0; i < ymbhs[0].split(",").length; i++) {
				if (i == ymbhs[0].split(",").length - 1) {
					condition += " '" + ymbhs[0].split(",")[i].trim() + "' )";
				} else {
					condition += " '" + ymbhs[0].split(",")[i].trim() + "',";
				}
			}
			condition += " or t.mrgn=2 ) ";
			sql = "select distinct t.* from appsys_qxygns t,"
					+ "(select t.jdlj as jdlj  from appsys_qxygns t  "
					+ "where t.scbz=0 and t.sfks=0 "
					+ condition
					+ " ) b "
					+ "where t.scbz=0 and t.sfks=0 and b.jdlj like  concat(t.jdlj,'%') "
					+ "order by t.ymbh";
			Session session = this.getSession();
			qxygnsList = session.createSQLQuery(sql).addEntity(
					AppsysQxygns.class).list();
			// session.close();
		}

		return qxygnsList;
	}

	/**
	 * @toDo:保存权限与功能树信息;
	 * @param appsysQxygns
	 * @param gnbhs
	 * @return boolean;
	 */
	public boolean addAppsysQxygns(AppsysQxygns appsysQxygns, String[] gnbhs) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			if (appsysQxygns != null) {
				appsysQxygns.setYmbh(getNextYmbh(appsysQxygns.getSjbh()));
				appsysQxygns.setJibie(new Long(
						appsysQxygns.getSjbh().length() / 2 + 1));
				appsysQxygns.setScbz(new Long(0));
				appsysQxygns.setSfks(new Long(0));
				appsysQxygns.setCjrq(DateUtils.currentDate());
				if (appsysQxygns.getCrj() == null) {
					appsysQxygns.setCrj("admin");
				}
				if (appsysQxygns.getCxrk() == null) {
					appsysQxygns.setCxrk("jsp/right.html");
				}

				Session session = this.getSession();
				// Transaction t = session.beginTransaction();
				// t.begin();

				// 更新上一级是不是节点字段;
				sql = "from com.td.framework.webmanager.domain.AppsysQxygns t "
						+ "where t.ymbh='" + appsysQxygns.getSjbh() + "'";
				AppsysQxygns parentQxygns = (AppsysQxygns) session.createQuery(
						sql).list().get(0);
				parentQxygns.setSbsjd(new Long(0));
				session.update(parentQxygns);

				appsysQxygns.setJdlj(parentQxygns.getJdlj() + "/"
						+ appsysQxygns.getYmbh());
				session.save(appsysQxygns);

				// 保存页面功能与模块关系
				AppsysYmygnsgx ymygnsgx = new AppsysYmygnsgx();
				if (gnbhs != null) {
					for (int i = 0; i < gnbhs.length; i++) {
						ymygnsgx = new AppsysYmygnsgx();
						ymygnsgx.setGnbh(Long.parseLong(gnbhs[i]));
						ymygnsgx.setYmbh(appsysQxygns.getYmbh());
						ymygnsgx.setScbz(new Long(0));
						session.save(ymygnsgx);
					}
				}
				// t.commit();
				// session.close();
				flag = true;
			}
		} catch (RuntimeException re) {
			flag = false;
			log.error("Exception： QxygnsDaoImpl+addAppsysQxygns", re);
			throw re;
		}
		return flag;
	}

	/**
	 * @toDo:更新权限与功能树信息;
	 * @param appsysQxygns
	 * @param gnbhs
	 * @return boolean;
	 */
	public boolean updateAppsysQxygns(AppsysQxygns appsysQxygns, String[] gnbhs) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			Session session = this.getSession();
			// Transaction t = session.beginTransaction();
			// t.begin();

			// 更新上一级是不是节点字段;
			String oldJdlj = appsysQxygns.getJdlj();// 原节点路径
			sql = "from com.td.framework.webmanager.domain.AppsysQxygns t "
					+ "where t.ymbh='" + appsysQxygns.getSjbh() + "'";
			AppsysQxygns parentQxygns = (AppsysQxygns) session.createQuery(sql)
					.list().get(0);
			parentQxygns.setSbsjd(new Long(0));
			appsysQxygns.setJdlj(parentQxygns.getJdlj() + "/"
					+ appsysQxygns.getYmbh());
			appsysQxygns.setJibie(parentQxygns.getJibie() + 1);

			session.update(appsysQxygns);
			session.update(parentQxygns);

			// 删除页面功能与模块关系
			sql = "delete from appsys_ymygnsgx t where t.ymbh ='"
					+ appsysQxygns.getYmbh() + "'";
			Connection con = session.connection();
			con.setAutoCommit(false);
			Statement st = con.createStatement();
			st.addBatch(sql);
			sql = "update appsys_qxygns t set t.jdlj = '"
					+ appsysQxygns.getJdlj() + "'||substr(t.jdlj,length('"
					+ oldJdlj + "')+1) where t.jdlj like '" + oldJdlj + "/%'";
			st.addBatch(sql);
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			// 更新页面功能与模块关系
			AppsysYmygnsgx ymygnsgx = new AppsysYmygnsgx();
			if (gnbhs != null) {
				for (int i = 0; i < gnbhs.length; i++) {
					ymygnsgx = new AppsysYmygnsgx();
					ymygnsgx.setGnbh(Long.parseLong(gnbhs[i]));
					ymygnsgx.setYmbh(appsysQxygns.getYmbh());
					ymygnsgx.setScbz(new Long(0));
					session.save(ymygnsgx);
				}
			}
			// t.commit();
			// session.close();
			flag = true;
		} catch (RuntimeException re) {
			flag = false;
			log.error("Exception： QxygnsDaoImpl+addAppsysQxygns", re);
			throw re;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error("Exception： QxygnsDaoImpl+addAppsysQxygns", e);
		}
		return flag;
	}

	/**
	 * toDo:权限与功能树排序;
	 * 
	 * @param ymbh
	 * @param pxh
	 * @return flag;
	 */
	public boolean orderQxygns(String[] ymbh, String[] pxh) {
		boolean flag = false;
		Connection con = null;
		try {
			con = getSession().connection();
			con.setAutoCommit(false);
			Statement st = con.createStatement();
			for (int i = 0; i < ymbh.length; i++) {
				sql = "update appsys_qxygns t set t.pxh=" + pxh[i]
						+ " where t.ymbh = " + ymbh[i];
				st.addBatch(sql);
			}
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			log.error("Exception： QxygnsDaoImpl+orderQxygns", e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					flag = false;
					log.error("Exception： QxygnsDaoImpl+orderQxygns", e);
				}
			}
		}
		return flag;
	}

	/**
	 * @toDo:生成下一个页面编号,每2个数字为一段;
	 * @param sjbh
	 * @return ymbh;
	 */
	@SuppressWarnings("unchecked")
	public String getNextYmbh(String sjbh) {
		String ymbh = "";
		if (sjbh.equals("-1")) {
			ymbh = "01";
		} else {
			sql = "select t.ymbh from com.td.framework.webmanager.domain.AppsysQxygns t where t.sjbh='"
					+ sjbh + "'";
			qxygnsList = this.getHibernateTemplate().find(sql);
			if (qxygnsList == null || qxygnsList.size() == 0) {
				ymbh = sjbh + "01";
			} else {
				int max = 0;// 最大值;
				int num = 0;// 临时存放数据;
				for (Iterator iterator = qxygnsList.iterator(); iterator
						.hasNext();) {
					ymbh = iterator.next() + "";
					num = Integer.parseInt(ymbh.substring(ymbh.length() - 2));
					if (num > max) {
						max = num;
					}
				}
				max = max + 1;
				if (max < 10) {
					ymbh = sjbh + "0" + max;
				} else {
					ymbh = sjbh + max;
				}

			}
		}
		return ymbh;
	}

	/**
	 * @toDo:验证页面名称是否存在;
	 * @param ymmc
	 * @param ymbh
	 * @param sjbh
	 * @return boolean;
	 */
	@SuppressWarnings("unchecked")
	public boolean checkQxygns(String ymmc, String ymbh, String sjbh) {
		boolean flag = false;
		sql = "from com.td.framework.webmanager.domain.AppsysQxygns t "
				+ "where t.ymmc='" + ymmc.trim() + "' and t.sjbh='" + sjbh
				+ "' ";
		if (!ymbh.trim().equals("") && !ymbh.trim().equals("null")) {
			sql = "from com.td.framework.webmanager.domain.AppsysQxygns t "
					+ "where t.ymmc='"
					+ ymmc.trim()
					+ "' and t.sjbh='"
					+ sjbh
					+ "' and t.ymbh!='" + ymbh + "' ";
		}
		qxygnsList = this.getHibernateTemplate().find(sql);
		if (!qxygnsList.isEmpty()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * toDo:功能树初始化
	 * 
	 * @param ymbhs
	 */
	@SuppressWarnings("unchecked")
	public String initGns(String ymbhs) {
		sql = " select 'insert into appsys_qxygns  (ID, YMBH, YMMC, SJBH, JIBIE, SBSJD, JDLJ,"
				+ " CXRK, BEIZHU, SFKS, SCBZ, CRJ, CJRQ,PXH) values ('||t.ID||', '''||t.YMBH||''', "
				+ "'''||t.YMMC||''', '''||t.SJBH||''', '||t.JIBIE||', "
				+ "'||t.SBSJD||', '''||t.JDLJ||''', '''||t.CXRK||''', '''||t.BEIZHU||''',"
				+ " '||t.SFKS||', '||t.SCBZ||', '''|| nvl(t.CRJ,'admin')||''',"
				+ "'||'to_date('''||to_char(t.CJRQ,'yyyymmdd')||''',''yyyy-mm-dd'')'||', '||t.PXH||');' as sql_str  "
				+ "from appsys_qxygns t where t.ymbh in ("
				+ ymbhs
				+ ") order by t.ymbh";
		StringBuffer initSql = new StringBuffer();
		initSql.append("set feedback off  \r\n" + "set define off  \r\nprompt "
				+ "Disabling triggers for APPSYS_QXYGNS...  \r\n"
				+ "alter table APPSYS_QXYGNS disable all triggers; \r\n"
				+ "prompt Deleting APPSYS_QXYGNS...  \r\n"
				+ "delete from APPSYS_QXYGNS;\r\ncommit;  \r\n"
				+ "prompt 提示载入 APPSYS_QXYGNS...  \r\n");
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		List<String> list = query.list();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			initSql.append((String) iterator.next() + "  \r\n");
		}
		// session.close();
		initSql
				.append("commit;\r\nprompt Enabling triggers for APPSYS_QXYGNS...  \r\n"
						+ "alter table APPSYS_QXYGNS enable all triggers;\r\n"
						+ "set feedback on \r\nset define on  \r\n");
		return initSql.toString();
	}

	/**
	 * @toDo:get删除记录标志;
	 * @return the delStatus
	 */
	public boolean isDelStatus() {
		return delStatus;
	}

	/**
	 * @toDo:set删除记录标志;
	 * @param delStatus
	 *            the delStatus to set
	 */
	public void setDelStatus(boolean delStatus) {
		this.delStatus = delStatus;
	}

	/**
	 * @toDo:getSql语句
	 * @return sql
	 */
	public String getSql() {
		return sql;
	}

	/**
	 * @toDo:setSql语句
	 * @param sjbh
	 * @param ymmc
	 */
	public void setSql(String sjbh, String ymmc) {
		sql = " from com.td.framework.webmanager.domain.AppsysQxygns t "
				+ "where t.scbz=0 and t.sfks=0 and t.sjbh='" + sjbh + "' ";
		String condition = "";// 条件
		if (!ymmc.trim().equals("") && !ymmc.trim().equals("null")) {
			condition = " and t.ymmc like '%" + ymmc.trim() + "%'";
		}
		sql += condition + " order by t.jibie,t.pxh,t.ymbh ";
	}
}
